 <template>
  <div class="bg_page">
    <header-bar title="卖了换钱" v-if="sourceFrom != 'aishangjie'" :interceptReturn="true" @return="goBack"></header-bar>
    <result-panel :result-data="resultData"></result-panel>
    <recommend-goods-list v-if="sourceFrom != 'aishangjie'" :list="goodsListData"></recommend-goods-list>
    <div class="no_data_block" v-if="goodsListData.length == 0 && !isLoading && sourceFrom != 'aishangjie'">
      <no-data-panel></no-data-panel>
    </div>
    <div>
      <infinite-load v-if="goodsListData.length > 0" :scroll-target="scrollTarget" :is-loading="isLoading" :tipsShow="tipsShow" :is-nomore="isNomre" @load="getNextData"></infinite-load>
    </div>
    
  </div>
</template>

<script>
import HeaderBar from "@/components/header.vue"
import infiniteLoad from '@/components/infiniteLoad.vue';
import RecommendGoodsList from '@/views/common/goodsList/recommendGoodsList.vue';
import ResultPanel from '@/views/common/resultPanel/resultPanel.vue';
import {mapState,mapActions} from 'vuex';
import utils from '@/libs/utils';
import native from '@/libs/native/index';
export default {
  props: {
   
  },
  components:{
    HeaderBar,
    infiniteLoad,
    ResultPanel,
    RecommendGoodsList
  },

  data() {
    return {
      scrollTarget: '#scrollPanel',
      isLoading: false,
      isNomre: false,
      tipsShow: true,

      currentPage:1,
      goodsListData:[],
      sourceFrom:'',
      goodsId:'',
      downObj:null
    }
  },
  computed: {
    ...mapState({}),
    resultData(){
      if(this.sourceFrom == 'aishangjie'){
        return {
          status:'success',
          text:'发布成功',
          subText:'建议您下载闲巢APP来管理您发布的商品以及未来的订单哦~',
          btns:[
            {
              text:'立即下载',
              cb:this.loadApp
            }
          ]
        }

      }else{
        return {
          status:'success',
          text:'发布成功',
          btns:[
            {
              text:'查看商品',
              cb:this.viewGoods
            },
            {
              text:'回到首页',
              cb:this.returnToIndex
            }
          ]
        };
      }

    },

  },
  created() {
     this.sourceFrom = this.$route.query.from || '';
     this.goodsId = this.$route.query.id;
     if(this.sourceFrom != 'aishangjie'){
       this.getRecommendAppGoodsList();
     }else{
       this.getDownloadUrl();
     }
  },
  filters:{
 
  },
  methods: {
    ...mapActions(['toggleLoading','showMsg','showModal']),
    getRecommendAppGoodsList(){
      if(this.currentPage == 1){
        this.toggleLoading({flag:true});
      };
      this.isLoading = true;
      this.$ajax.getRecommendAppGoodsList({
        queryBody:{
          // status:'sale',
          // isNew:'Y',
          // source:'aishangjie'
        },
        page:{
          size:10,
          current:this.currentPage
        }
      }).then((res)=>{
        this.toggleLoading({flag:false});
        if(this.isLoading){
          this.isLoading = false;
        };
        if(res.code == '0000'){
          if(!this.tipsShow){
            this.tipsShow = true;
          };
         
          if(res.data.records.length < 10){
            this.isNomre = true;
          };
          if(this.currentPage == 1){
            this.goodsListData = res.data.records;
          }else{
            this.goodsListData = [...this.goodsListData,...res.data.records];
          }
        }
        
      });
    },
    getDownloadUrl(){
      this.$ajax.getDownloadUrl({}).then((res)=>{
        if(res.code == '0000'){
          this.downObj = res.data;
        }
      });
    },
    getNextData(){
      this.isLoading = true;
      this.currentPage++;
      this.getRecommendAppGoodsList()
    },

    viewGoods(){
      if(!this.goodsId){
         this.showMsg({content:'商品发布中，请稍后查看'});
         return;
      };
      native.goGoodsDetail(this.goodsId);
    },

    returnToIndex(){
      native.goHome();
    },

    loadApp(){
   
      let flag = utils.getBlatFrom();
      if(flag == 1 || flag == 3){
        if(this.downObj && this.downObj.android){
          window.location.href = this.downObj.android;
        }else{
          this.showMsg({content:'暂无下载地址'});
        }
      }else if(flag == 2 || flag == 4){
        if(this.downObj && this.downObj.ios){
          window.location.href = this.downObj.ios;
          // let iframe = document.createElement('iframe');
          // iframe.style.display = 'none';
          // iframe.src = this.downObj.ios;
          // document.body.appendChild(iframe);
        }else{
          this.showMsg({content:'暂无下载地址'});
        }
      }else{
        console.log(flag);
      };

    },

    goBack(){
      let originType = this.$route.query.originType;
      if(originType == 'h5'){
        this.$router.go(-1);
      }else{
        native.closeWebPage();
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .bg_page{
    background-color: #F6F6F6;
    min-height: 100%;
  }
  .no_data_block{
    padding-top: 1.6rem;
  }

</style>
